package com.houger.ddd.datamanager.utils;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

public class PackageRenamer {
    public static byte[] changePackage(byte[] originalBytes, String newPackage) {
        ClassReader reader = new ClassReader(originalBytes);
        ClassWriter writer = new ClassWriter(reader, 0);
        
        ClassVisitor visitor = new ClassVisitor(Opcodes.ASM9, writer) {
            @Override
            public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
                String newName = newPackage.replace(".", "/") + "/" + name.substring(name.lastIndexOf("/") + 1);
                super.visit(version, access, newName, signature, superName, interfaces);
            }
        };
        
        reader.accept(visitor, 0);
        return writer.toByteArray();
    }
}